package com.qiyun.framework.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Build;

import java.io.ByteArrayInputStream;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import javax.security.auth.x500.X500Principal;

/**
 * Created by mac on 16/2/29.
 */
public class SystemUtils {
        public static final int DEFAULT_THREAD_POOL_SIZE = getDefaultThreadPoolSize();
        private static final int JELLY_BEAN_MR1 = 17;
        private static final int JELLY_BEAN_MR2 = 18;
        private static final int KITKAT = 19;
        public static final int KITKAT_WATCH = 20;
        public static final int LOLLIPOP = 21;
        private static final X500Principal DEBUG_DN = new X500Principal("CN=Android Debug,O=Android,C=US");

        public SystemUtils() {
        }

        public static int getDefaultThreadPoolSize() {
            return getDefaultThreadPoolSize(8);
        }

        public static int getDefaultThreadPoolSize(int max) {
            int availableProcessors = 2 * Runtime.getRuntime().availableProcessors() + 1;
            return availableProcessors > max?max:availableProcessors;
        }

        public static String getSystemVersion() {
            return Build.VERSION.RELEASE;
        }

        public static boolean hasEclairMR1() {
            return Build.VERSION.SDK_INT >= 7;
        }

        public static boolean hasFroyo() {
            return Build.VERSION.SDK_INT >= 8;
        }

        public static boolean hasGingerbread() {
            return Build.VERSION.SDK_INT >= 9;
        }

        public static boolean hasHoneycomb() {
            return Build.VERSION.SDK_INT >= 11;
        }

        public static boolean hasHoneycombMR1() {
            return Build.VERSION.SDK_INT >= 12;
        }

        public static boolean hasHoneycombMR2() {
            return Build.VERSION.SDK_INT >= 13;
        }

        public static boolean hasIceCreamSandwich() {
            return Build.VERSION.SDK_INT >= 14;
        }

        public static boolean hasJellyBean() {
            return Build.VERSION.SDK_INT >= 16;
        }

        public static boolean hasJellyBeanMR1() {
            return Build.VERSION.SDK_INT >= 17;
        }

        public static boolean hasJellyBeanMR2() {
            return Build.VERSION.SDK_INT >= 18;
        }

        public static boolean hasKitKat() {
            return Build.VERSION.SDK_INT >= 19;
        }

        public static boolean hasKitKatWatch() {
            return Build.VERSION.SDK_INT >= 20;
        }

        public static boolean hasLollipop() {
            return Build.VERSION.SDK_INT >= 21;
        }

        public static boolean isDebuggable(Context ctx) {
            boolean debuggable = false;

            try {
                PackageInfo e = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 64);
                Signature[] signatures = e.signatures;

                for(int i = 0; i < signatures.length; ++i) {
                    CertificateFactory cf = CertificateFactory.getInstance("X.509");
                    ByteArrayInputStream stream = new ByteArrayInputStream(signatures[i].toByteArray());
                    X509Certificate cert = (X509Certificate)cf.generateCertificate(stream);
                    debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
                    if(debuggable) {
                        break;
                    }
                }
            } catch (PackageManager.NameNotFoundException var8) {
                ;
            } catch (CertificateException var9) {
                ;
            }

            return debuggable;
        }
}
